package com.meet.service;

import com.meet.dao.BoardRoomDao;
import com.meet.dao.BoardRoomDaoImpl;
import com.meet.entity.BoardRoom;
import com.meet.entity.PageBean;
import com.meet.vo.BoardRoomTimeVo;

import java.sql.SQLException;
import java.util.List;

/**
 * @author RicardoZeng
 * @date 2022/8/11 21:15
 */
public class BoardRoomServiceImpl implements BoardRoomService{
    BoardRoomDao boardRoomDao  = new BoardRoomDaoImpl();

    public BoardRoomServiceImpl(BoardRoomDao boardRoomDao) {
        this.boardRoomDao = boardRoomDao;
    }

    @Override
    public List<BoardRoom> getAllRooms(int currentPage, int rows) throws SQLException {
        return boardRoomDao.getAllRooms(currentPage, rows);
    }

    @Override
    public int addBoardRoom(BoardRoom boardRoom) throws SQLException {
        if(boardRoom == null)
            throw new SQLException("数据为空！！！");
        return boardRoomDao.addBoardRoom(boardRoom);
    }

    @Override
    public int deleteBoardRoom(Integer Id) throws SQLException {
        if(Id <=0)
            throw new SQLException("Id 不能小于或等于0！！！");
        return boardRoomDao.deleteBoardRoom(Id);
    }

    @Override
    public int updateBoardRoom(BoardRoom boardRoom) throws SQLException {
        if(boardRoom == null)
            throw new SQLException("数据为空！！！");
        return boardRoomDao.updateBoardRoom(boardRoom);
    }

    @Override
    public int count() throws SQLException {
        return boardRoomDao.count();
    }

    @Override
    public BoardRoom getRoom(Integer Id) throws SQLException {
        if(Id <=0)
            throw new SQLException("Id 不能小于或等于0！！！");
        return boardRoomDao.getRoom(Id);
    }


    @Override
    public PageBean<BoardRoom> getAllRoomsByPage(String _currentPage, String _rows) throws SQLException {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        if(currentPage <=0) {
            currentPage = 1;
        }

        //1.创建空的PageBean对象
        PageBean<BoardRoom> pb = new PageBean<BoardRoom>();
        //2.设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3.调用dao查询总记录数
        int totalCount = boardRoomDao.count();
        pb.setTotalCount(totalCount);

        //4.调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<BoardRoom> list = boardRoomDao.getAllRooms(start,rows);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows)  == 0 ? totalCount/rows : (totalCount/rows) + 1;
        pb.setTotalPage(totalPage);


        return pb;
    }

    public List<BoardRoomTimeVo> getBoardRoomTime(Integer id) throws SQLException {
        return boardRoomDao.getBoardRoomTime(id);
    }




}
